import { defineStore } from "pinia";
import { Reactive, reactive, Ref, ref } from "vue";
import { get, post } from "@/request";
import { ElMessage } from "element-plus";

interface current_row {
  base_name: string;
  base_deptname: string;
  sericnum: string;
  name: string;
  nameen: string;
  pid: string;
  fuzename: string;
  city: string;
  address: string;
  tel: string;
  fax: string;
  sort: string;
  id: string;
  edit: string;
  sort_num: string;
}

interface res {
  data: current_row[];
  page: number;
  total: number;
}

export const useUserStore = defineStore("User", () => {
  const units: Reactive<current_row[]> = reactive<current_row[]>(
    [] as current_row[]
  );
  const current_row: Reactive<current_row> = reactive<current_row>(
    {} as current_row
  );
  const page = ref(1);
  const total = ref(1);
  const search_val = ref("");
  const search_obj = ref([
    { label: "姓名", key: "name", value: "" },
    { label: "用户名", key: "address", value: "" },
    { label: "入职日期", key: "address", value: "" },
    { label: "手机号", key: "address", value: "" },
    { label: "部门", key: "address", value: "" },
    { label: "多部门", key: "address", value: "" },
    { label: "多职位", key: "address", value: "" },
    { label: "上级主管", key: "address", value: "" },
    { label: "邮箱", key: "address", value: "" },
    { label: "启用", key: "address", value: "" },
  ]);
  const columns_show: Ref = ref([
    { label: "申请人(base_name)", value: true },
    { label: "申请人部门(base_deptname)", value: true },
    { label: "单号(sericnum)", value: true },
    { label: "头像(face)", value: true },
    { label: "姓名(name)", value: true },
    { label: "密码(pass)", value: false },
    { label: "编号(num)", value: true },
    { label: "入职日期(workdate)", value: true },
    { label: "性别(sex)", value: true },
    { label: "手机号(mobile)", value: true },
    { label: "部门(departname)", value: true },
    { label: "职位(ranking)", value: true },
    { label: "多部门(departnames)", value: true },
    { label: "上级主管(supername)", value: true },
    { label: "上级主管id(superid)", value: true },
    { label: "名字拼音(pingyin)", value: true },
    { label: "电话(tel)", value: true },
    { label: "邮箱(email)", value: true },
    { label: "微信号(weixinid)", value: true },
    { label: "所属单位(companyid)", value: true },
    { label: "启用(status)", value: true },
    { label: "管理员(type)", value: true },
    { label: "用户id(id)", value: true },
    { label: "排序号(sort)", value: true },
    { label: "所在组(groupname)", value: true },
    { label: "操作列", value: true },
  ]);
  const get_units = async (options = {}) => {
    const data: res = await get("/system/baseSet/unit", options);
    units.splice(0, units.length, ...data.data);
    page.value = data.page;
    total.value = data.total;
  };
  const unit_add = async () => {
    await post("/system/baseSet/unit/add", current_row);
    await get_units();
  };
  const on_search = async () => {
    await get_units({
      name: search_val.value,
    });
  };
  const on_up_search = async () => {
    const options = {};
    search_obj.value.forEach((item) => {
      Object.assign(options, { [item.key]: item.value });
    });
    await get_units(options);
  };
  const go = async () => {
    const to_page = Number(page.value) + 1;
    if (to_page > total.value) {
      ElMessage.warning("已经是最后一页了");
      return;
    }
    await get_units({ page: to_page });
  };
  const back = async () => {
    const to_page = Number(page.value) - 1;
    if (to_page < total.value) {
      ElMessage.warning("已经是第一页了");
      return;
    }
    await get_units({ page: to_page });
  };
  const to_head = async () => {
    await get_units({ page: 1 });
  };
  const to_end = async () => {
    await get_units({ page: Number(total.value) });
  };
  const refresh = async () => {
    await get_units();
  };
  return {
    units,
    current_row,
    get_units,
    unit_add,
    columns_show,
    on_search,
    search_val,
    search_obj,
    on_up_search,
    page,
    total,
    go,
    back,
    to_head,
    to_end,
    refresh,
  };
});
